import inspect

from pydantic import conint

from common.base.base_api import BaseApi
from services.automatic_control.schemas.configs import PilotTelemetry
from services.server_integration.schemas.calibrate_status import CalibrateHistory
from settings import settings


class AutomaticControlApi(BaseApi):
    __slots__ = []

    def __init__(self):
        super(AutomaticControlApi, self).__init__(
            request_queue_uuid=settings.AUTOMATIC_CONTROL_REQUEST_QUEUE,
        )

    async def link_vswr_calibrate(self, index: int) -> float | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def run_service_tx_ac(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def set_loops_states(
            self, link_tx_loop_state: bool, link_rx_loop_state: bool, service_tx_loop_state: bool, service_rx_loop_state: bool
    ) -> bool:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'link_tx_loop_state': link_tx_loop_state,
                'link_rx_loop_state': link_rx_loop_state,
                'service_tx_loop_state': service_tx_loop_state,
                'service_rx_loop_state': service_rx_loop_state
            }
        )

    async def get_current_output_power(self, index: int) -> float:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'index': index,
            }
        )

    async def set_output_power_correct_number(self, index: int, number: float):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'index': index,
                'number': number
            }
        )

    async def set_additional_output_power_correct_number(self, index: int, number: float):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'index': index,
                'number': number
            }
        )

    async def get_loops_states(
            self, link_tx_loop_state: bool, link_rx_loop_state: bool, service_tx_loop_state: bool
    ) -> bool:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'link_tx_loop_state': link_tx_loop_state,
                'link_rx_loop_state': link_rx_loop_state,
                'service_tx_loop_state': service_tx_loop_state
            }
        )

    async def get_output_power_correct_number(self, index: int):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'index': index,
            }
        )

    async def get_additional_output_power_correct_number(self, index: int):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'index': index,
            }
        )

    async def set_sm_base_power(self, sm_base_power: list[float]):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'sm_base_power': sm_base_power},
            timeout=11*60
        )

    async def set_rm_base_power(self, rm_base_power: list[float]):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'rm_base_power': rm_base_power})

    async def set_loop_state(self, state: bool) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'state': state})

    async def get_loop_state(self) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def set_adjustment_state(self, state: bool) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'state': state})

    async def get_adjustment_state(self) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def get_pilot_telemetry(self) -> PilotTelemetry:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda r: PilotTelemetry(**r)
        )

    async def get_calibrate_history(self) -> CalibrateHistory:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda r: CalibrateHistory(**r)
        )

    async def stop_calibration(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def calibrate(
            self,
            sm_base_power: list[float], sm_cable_loss_dl: list[float], sm_state: list[bool],
            rm_output_power: list[float], rm_cable_loss: list[float], rm_state: list[bool],
            acc_base_power_calibrate: bool, acc_link_calibrate: bool, acc_tx_service_calibrate: bool,
            only_reach_the_power: bool = False
            ) -> bool:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'sm_base_power': sm_base_power,
                'sm_cable_loss_dl': sm_cable_loss_dl,
                'sm_state': sm_state,
                'rm_output_power': rm_output_power,
                'rm_cable_loss': rm_cable_loss,
                'rm_state': rm_state,
                'acc_base_power_calibrate': acc_base_power_calibrate,
                'acc_link_calibrate': acc_link_calibrate,
                'acc_tx_service_calibrate': acc_tx_service_calibrate,
                'only_reach_the_power': only_reach_the_power
            },
            timeout=25*60
        )

    async def get_actual_reference_freq_40mhz(self) -> float | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def read_input_power(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_output_power(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_rx(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_rx_nl(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_pa_rx(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_pa_rx_nl(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_rx_a(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_rx_b(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_rx_a_nl(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_rx_b_nl(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_threshold(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def read_pilot_power_tx(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def enforce_sm_tx_ac(self, index: int, count: int = 3):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index, 'count': count})

    async def read_true_power(self, index: int) -> float:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def sync_ad(self, index: int):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def carrier_frequency_control_enable(self, enable: bool):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'enable': enable})

    async def read_status_of_link(self, index: int) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def calibration_in_progress(self) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def calibration_in_progress_time(self) -> float | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def carrier_frequency_control(self, freq: float) -> bool:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'freq': freq})

    async def get_force_find_pilot(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def set_force_find_pilot(self, state: bool):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'state': state})

